/*
 * FreeModbus Libary: BARE Port
 * Copyright (C) 2006 Christian Walter <wolti@sil.at>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * File: $Id$
 */

#include "port.h"

/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"
#include "fm33lg0xx_fl.h"
#include "uart.h"

/* ----------------------- static functions ---------------------------------*/
static void prvvUARTTxReadyISR( void );
static void prvvUARTRxISR( void );

/* ----------------------- Start implementation -----------------------------*/
void
vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
{
    /* If xRXEnable enable serial receive interrupts. If xTxENable enable
     * transmitter empty interrupts.
     */
    if( xRxEnable )
    {
        FL_UART_ClearFlag_RXBuffFull(UART0);
        FL_UART_EnableIT_RXBuffFull(UART0);
    }
    else
    {
        FL_UART_DisableIT_RXBuffFull(UART0);
    }

    if( xTxEnable )
    {
        FL_UART_ClearFlag_TXShiftBuffEmpty(UART0);
        FL_UART_EnableIT_TXShiftBuffEmpty(UART0);
    }
    else
    {
        FL_UART_DisableIT_TXShiftBuffEmpty(UART0);
    }
}

BOOL
xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
{
    FL_GPIO_InitTypeDef    GPIO_InitStruct;

    FL_UART_InitTypeDef    UART0_InitStruct;

    /* PA13 UART0_RX */
    GPIO_InitStruct.pin = FL_GPIO_PIN_13;
    GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull = FL_DISABLE;
    GPIO_InitStruct.remapPin = FL_DISABLE;
    GPIO_InitStruct.analogSwitch = FL_DISABLE;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* PA14 UART0_TX */
    GPIO_InitStruct.pin = FL_GPIO_PIN_14;
    GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull = FL_DISABLE;
    GPIO_InitStruct.remapPin = FL_DISABLE;
    GPIO_InitStruct.analogSwitch = FL_DISABLE;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    UART0_InitStruct.clockSrc = FL_CMU_UART0_CLK_SOURCE_APBCLK;
    UART0_InitStruct.baudRate = ulBaudRate;
    UART0_InitStruct.transferDirection = FL_UART_DIRECTION_TX_RX;
    UART0_InitStruct.dataWidth = FL_UART_DATA_WIDTH_8B;
    UART0_InitStruct.stopBits = FL_UART_STOP_BIT_WIDTH_1B;
    UART0_InitStruct.parity = FL_UART_PARITY_NONE;

    FL_UART_Init(UART0, &UART0_InitStruct);

    // FL_UART_ClearFlag_RXBuffFull(UART0);
    // FL_UART_EnableIT_RXBuffFull(UART0);

    // FL_UART_ClearFlag_TXShiftBuffEmpty(UART0);
    // FL_UART_EnableIT_TXShiftBuffEmpty(UART0);

    // FL_UART_ClearFlag_RXBuffTimeout(UART0);
    // FL_UART_EnableIT_RXTimeout(UART0);

    NVIC_SetPriority(UART0_IRQn, 0x02);
    NVIC_EnableIRQ(UART0_IRQn);
    return TRUE;
}

BOOL
xMBPortSerialPutByte( CHAR ucByte )
{
    /* Put a byte in the UARTs transmit buffer. This function is called
     * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been
     * called. */
    FL_UART_WriteTXBuff(MB_PORT, ucByte);
    return TRUE;
}

BOOL
xMBPortSerialGetByte( CHAR * pucByte )
{
    /* Return the byte in the UARTs receive buffer. This function is called
     * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
     */
    *pucByte = FL_UART_ReadRXBuff(MB_PORT);
    return TRUE;
}

/* Create an interrupt handler for the transmit buffer empty interrupt
 * (or an equivalent) for your target processor. This function should then
 * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
 * a new character can be sent. The protocol stack will then call
 * xMBPortSerialPutByte( ) to send the character.
 */
static void prvvUARTTxReadyISR( void )
{
    pxMBFrameCBTransmitterEmpty(  );
}

/* Create an interrupt handler for the receive interrupt for your target
 * processor. This function should then call pxMBFrameCBByteReceived( ). The
 * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
 * character.
 */
static void prvvUARTRxISR( void )
{
    pxMBFrameCBByteReceived(  );
}

void UART0_IRQHandler()
{
    if(FL_UART_IsActiveFlag_RXBuffFull(UART0))
    {
        prvvUARTRxISR();
    }
    if(FL_UART_IsActiveFlag_TXShiftBuffEmpty(UART0))
    {
        FL_UART_ClearFlag_TXShiftBuffEmpty(UART0);
        prvvUARTTxReadyISR();
    }

    // if(FL_UART_IsActiveFlag_RXBuffTimeout(UART0))
    // {
    //     FL_UART_ClearFlag_RXBuffTimeout(UART0);
    //     prvvTIMERExpiredISR();
    // }

}
